package com.tstudy.laoshibang.camera;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import com.edmodo.cropper.CropImageView;
import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.BaseFragment;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.event.SelectPictureEvent;
import com.tstudy.laoshibang.event.UploadAvatarEvent;
import com.tstudy.laoshibang.utils.CommonUtil;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import de.greenrobot.event.EventBus;
/**
 * 裁剪
* @CopyRight xinweitech.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年4月9日 下午6:06:28
 */
@EFragment(R.layout.cropper)
public class CropperFragment extends BaseFragment {
	public static String TAG = "cropper";
	private static final int DEFAULT_ASPECT_RATIO_VALUES = 60;
	@ViewById(R.id.cropper_root)
	View mRootLayout;
	RelativeLayout mBottomLayout;
//	private boolean mIsPortrait;
	@ViewById(R.id.cropper_imageview)
	CropImageView mCropImageView;
	
	boolean mIsFirstLoad = true;
	private Bitmap mCroppedBitmap;
	int mOrientation;
	Handler mHandler;
	boolean mIsPortrait;
	public CropperFragment() {
		super();
	}

	public static void add(int containerViewId,boolean isPortrait,int orientation){
		Bundle args = new Bundle();
		args.putBoolean(CONSTANT.ARGS.IS_PORTRAIT, isPortrait);
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID,containerViewId);
		args.putInt(CONSTANT.ARGS.ORIENTATION, orientation);
		Fragment fragment = Fragment.instantiate(BaseApplication.mContext,CropperFragment_.class.getName(), args);
		((BaseActivity)BaseApplication.mCurrentActivity).add(containerViewId,fragment,TAG);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		mHandler = new Handler();
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup)mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}

	/**
	 * Set more information data.
	 * 
	 * @param fragment
	 */
	
	@AfterViews
	public void initView() {
		if (mIsFirstLoad) {
			mCropImageView.setImageBitmap(BaseApplication.mCameraBM,mOrientation);
			mCropImageView.setAspectRatio(DEFAULT_ASPECT_RATIO_VALUES, DEFAULT_ASPECT_RATIO_VALUES);
			mCropImageView.setGuidelines(2);
			mCropImageView.setLeft(0);
		}
	}
	
	@Click({R.id.cropper_button_cancel,R.id.cropper_button_crop,R.id.cropper_button_rotate})
	public void clickAction(View v) {
		switch (v.getId()) {
		case R.id.cropper_button_cancel:
			backAction(mFragmentId);
			break;
		case R.id.cropper_button_crop:
			mCroppedBitmap = mCropImageView.getCroppedImage();
			final String filePath = CommonUtil.saveImage(CommonUtil.getUploadPicturePath(),mCroppedBitmap);
			if (filePath != null) {
				if (mIsPortrait) {
					UploadAvatarEvent event = new UploadAvatarEvent();
					event.type = "profile";
					event.filePath = filePath;
					EventBus.getDefault().post(event);
				}else{
					SelectPictureEvent event = new SelectPictureEvent();
					event.filePath = filePath;
					EventBus.getDefault().post(event);
				}
				getActivity().finish();
//				backAction(mFragmentId);
//				backAction(mFragmentId);
			}else{
				BaseApplication.showToast("picture cropper failed");
			}
			break;
		case R.id.cropper_button_rotate:
			mCropImageView.setImageBitmap(mCropImageView.getImageBitMap(),90);
			break;
		default:
			break;
		}
	}
	
	

	

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		mCropImageView.setImageBitmap(null);
//		CommonUtil.recycleBm(BaseApplication.mCameraBM);
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mIsPortrait = args.getBoolean(CONSTANT.ARGS.IS_PORTRAIT);
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
			mOrientation = args.getInt(CONSTANT.ARGS.ORIENTATION);
		}
	}

}
